package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.system.dtos.request.SysUpdateStatusReq;
import com.rightsidetech.system.dtos.request.store.SysStoreLoginInfoAddReq;
import com.rightsidetech.system.dtos.request.store.SysStoreLoginInfoReq;
import com.rightsidetech.system.dtos.request.store.SysStoreLoginInfoUpdateReq;
import com.rightsidetech.system.dtos.response.agent.SysDropDownNameResp;
import com.rightsidetech.system.dtos.response.store.SysStoreLoginInfoResp;
import com.rightsidetech.system.entity.SysAgentLoginInfo;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.entity.store.SysStoreLoginInfo;
import com.rightsidetech.system.mapper.SysAgentLoginInfoMapper;
import com.rightsidetech.system.mapper.SysStoreInfoMapper;
import com.rightsidetech.system.mapper.SysStoreLoginHistoryMapper;
import com.rightsidetech.system.mapper.SysStoreLoginInfoMapper;
import com.rightsidetech.system.service.SysStoreLoginInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2021-11-03 16:13
 */
@Service("sysStoreLoginInfoServiceImpl")
public class SysStoreLoginInfoServiceImpl implements SysStoreLoginInfoService {
    @Resource
    private SysStoreLoginInfoMapper sysStoreLoginInfoMapper;
    @Resource
    private SysStoreInfoMapper sysStoreInfoMapper;
    @Resource
    private SysAgentLoginInfoMapper sysAgentLoginInfoMapper;
    @Resource
    private SysStoreLoginHistoryMapper sysStoreLoginHistoryMapper;


    @Override
    public ResponseData<PageInfo<SysStoreLoginInfoResp>> page(SysStoreLoginInfoReq req, Long loginInfoId) {
        SysAgentLoginInfo loginInfo = sysAgentLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (StringUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<SysStoreLoginInfoResp> list = sysStoreLoginInfoMapper.page(req,loginInfo.getAgentInfoId());
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData saveStoreLoginInfo(SysStoreLoginInfoAddReq req, Long loginInfoId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysAgentLoginInfo loginInfo = sysAgentLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (StringUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        SysStoreInfo sysStoreInfo = sysStoreInfoMapper.selectByAgentInfoId(req.getStoreId(), loginInfo.getAgentInfoId());
        if (ObjectUtils.isEmpty(sysStoreInfo)){
            return ResponseData.fail(CodeIdEnum.NonStoreInfo);
        }
        List<Long> longList = sysStoreLoginInfoMapper.selectNameMobile(req.getLoginName(), req.getMobileNo());
        if (!ObjectUtils.isEmpty(longList) || longList.size() > 0){
            return ResponseData.fail("系统中已存在帐号或手机号");
        }
        SysStoreLoginInfo infoName = sysStoreLoginInfoMapper.selectByName(req.getLoginName());
        if (!ObjectUtils.isEmpty(infoName)){
            return ResponseData.fail(CodeIdEnum.EXISTACCOUNT);
        }
        SysStoreLoginInfo info = new SysStoreLoginInfo();
        info.setLoginName(req.getLoginName());
        info.setSalt(String.valueOf(Math.random() * 900000 + 100000));
        info.setPassword(DigestUtils.md5DigestAsHex((req.getPassword() + info.getSalt()).getBytes()));
        info.setStoreId(req.getStoreId());
        info.setUserName(req.getUserName());
        info.setMobileNo(req.getMobileNo());
        info.setCreaterId(loginInfoId);
        info.setCreatedAt(new Date());

        sysStoreLoginInfoMapper.insertSelective(info);
        return ResponseData.success();
    }

    @Override
    public ResponseData updateStoreLoginInfo(SysStoreLoginInfoUpdateReq req, Long loginInfoId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreLoginInfo storeLoginInfo = sysStoreLoginInfoMapper.selectByPrimaryKey(req.getId());
        if (StringUtils.isEmpty(storeLoginInfo)){
            return ResponseData.fail("门店帐号不存在");
        }
        SysAgentLoginInfo loginInfo = sysAgentLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (StringUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        SysStoreInfo sysStoreInfo = sysStoreInfoMapper.selectByAgentInfoId(req.getStoreId(), loginInfo.getAgentInfoId());
        if (ObjectUtils.isEmpty(sysStoreInfo)){
            return ResponseData.fail(CodeIdEnum.NonStoreInfo);
        }
        List<Long> longList = sysStoreLoginInfoMapper.selectNameMobile(req.getLoginName(), req.getMobileNo());
        for (Long id:longList) {
            if (!id.equals(req.getId())){
                return ResponseData.fail("系统中已存在帐号或手机号");
            }
        }

        SysStoreLoginInfo info = new SysStoreLoginInfo();
        info.setId(req.getId());
        info.setLoginName(req.getLoginName());
        if (!StringUtils.isEmpty(req.getPassword())){
            info.setSalt(String.valueOf(Math.random() * 900000 + 100000));
            info.setPassword(DigestUtils.md5DigestAsHex((req.getPassword() + info.getSalt()).getBytes()));
        }
        info.setStoreId(req.getStoreId());
        info.setUserName(req.getUserName());
        info.setMobileNo(req.getMobileNo());
        sysStoreLoginInfoMapper.updateByPrimaryKeySelective(info);
        return ResponseData.success();
    }

    @Override
    public ResponseData updateStatus(SysUpdateStatusReq req, Long loginInfoId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreLoginInfo info = sysStoreLoginInfoMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(info)){
            return ResponseData.fail(CodeIdEnum.NOACCOUNT);
        }
        List<Long> longs = getStoreId(loginInfoId);
        if (ObjectUtils.isEmpty(longs) || !longs.contains(req.getId())){
            return ResponseData.fail(CodeIdEnum.NOACCOUNT);
        }
        if (info.getStatus().equals(req.getStatus())){
            return ResponseData.fail(CodeIdEnum.REPEATSTATUS);
        }
        sysStoreLoginInfoMapper.updateStatus(req.getId(),req.getStatus());
        return ResponseData.success();
    }

    @Override
    public ResponseData del(DelReq req, Long loginInfoId) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysStoreLoginInfo info = sysStoreLoginInfoMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(info) || info.getIsDeleted() == 1){
            return ResponseData.fail(CodeIdEnum.NOACCOUNT);
        }
        List<Long> longs = getStoreId(loginInfoId);
        if (ObjectUtils.isEmpty(longs) || !longs.contains(req.getId())){
            return ResponseData.fail(CodeIdEnum.NOACCOUNT);
        }
        sysStoreLoginInfoMapper.del(req.getId());
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<SysDropDownNameResp>> selectList(Long loginInfoId) {
        SysAgentLoginInfo sysAgentLoginInfo = sysAgentLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (ObjectUtils.isEmpty(sysAgentLoginInfo)){
            return ResponseData.fail(CodeIdEnum.NOACCOUNT);
        }
        return ResponseData.success(sysStoreLoginInfoMapper.selectUserName(sysAgentLoginInfo.getAgentInfoId()));
    }

    @Override
    public SysStoreLoginInfo selectByLoginInfoId(Long loginInfoId) {
        return sysStoreLoginInfoMapper.selectByPrimaryKey(loginInfoId);
    }

    private List<Long> getStoreId(Long loginInfoId){
        SysAgentLoginInfo sysAgentLoginInfo = sysAgentLoginInfoMapper.selectByPrimaryKey(loginInfoId);
        if (ObjectUtils.isEmpty(sysAgentLoginInfo)){
            return null;
        }
        List<Long> longs = sysStoreLoginInfoMapper.findByAgentInfoId(sysAgentLoginInfo.getAgentInfoId());
        return longs;
    }

    @Override
    public SysStoreLoginInfoResp selectById(Long id) {
        return sysStoreLoginInfoMapper.selectById(id);
    }
}
